<template>
  <a-upload-dragger
    v-model:fileList="fileList"
    name="files"
    :multiple="multiple"
    :action="baseUrl + action"
    :data="{
      resourcesId: resourceId,
      dirStatementId: dirStatementId,
      dirId: dirId,
      type: reportType
    }"
    :remove="remove"
    @change="handleChange"
  >
    <p class="ant-upload-drag-icon">
      <inbox-outlined></inbox-outlined>
    </p>
    <p class="ant-upload-text">点击或将文件拖拽到这里上传</p>
    <p class="ant-upload-hint"> 支持扩展名：.rar .zip .doc .docx .pdf .jpg... </p>
  </a-upload-dragger>
</template>
<script lang="ts">
  import { InboxOutlined } from '@ant-design/icons-vue'
  import { message, Upload } from 'ant-design-vue'
  import { defineComponent, ref } from 'vue'
  import { deleteResourceFile } from '@/api/resource'
  import { deleteReportingFile } from '@/api/report'
  import { deleteDirectoryTemplate } from '@/api/directory'

  interface FileItem {
    uid: string
    name?: string
    status?: string
    response?: any
    url?: string
    [key: string]: any
  }

  interface FileInfo {
    file: FileItem
    fileList: FileItem[]
  }

  export default defineComponent({
    name: 'CustomUpload',
    components: {
      InboxOutlined,
      [Upload.name]: Upload,
      [Upload.Dragger.name]: Upload.Dragger
    },
    props: {
      multiple: {
        type: Boolean,
        default: true
      },
      resourceId: {
        type: [String],
        default: ''
      },
      dirStatementId: {
        type: [String],
        default: ''
      },
      reportType: {
        type: Number,
        default: 2
      },
      dirId: {
        type: [String],
        default: ''
      },
      action: {
        type: String,
        default: ''
      },
      baseUrl: {
        type: String,
        default: import.meta.env.VITE_APP_API_URL
      }
    },
    emits: ['update:resourceId', 'update:dirId'],
    setup(props, { emit }) {
      const handleChange = (info: FileInfo) => {
        console.log(info)
        const status = info.file.status
        if (status === 'done') {
          if (info?.file?.response?.status === 200) {
            message.success(`${info.file.name}  上传成功！`)
            emit('update:resourceId', info?.file?.response?.data?.resourceId || '')
            emit('update:dirId', info?.file?.response?.data?.dirId || '')
          } else {
            message.error(`${info.file.name} 上传失败！`)
          }
        } else if (status === 'error') {
          message.error(`${info.file.name} 上传失败！`)
        }
      }

      const remove = async (file) => {
        if (props.resourceId.length) {
          try {
            const resourceRes = await deleteResourceFile(file?.response?.data?.fileId)
            return !!resourceRes
          } catch (e) {
            return false
          }
        }

        if (props.dirStatementId.length) {
          try {
            const reportRes = await deleteReportingFile(file?.response?.data?.dirStatementId)
            return !!reportRes
          } catch (e) {
            return false
          }
        }

        if (props.dirId.length) {
          try {
            const directoryRes = await deleteDirectoryTemplate(file?.response?.data?.dirId)
            return !!directoryRes
          } catch (e) {
            return false
          }
        }
      }

      return {
        handleChange,
        remove,
        fileList: ref([])
      }
    }
  })
</script>
